#include "EDebug.h"

#include "eCPP.h"
namespace engine
{
//------------------------------------------------------------
// EDebugDrawable
//------------------------------------------------------------

EDebugDrawable::EDebugDrawable() :
m_bDebugStarted( false ),
m_cColor( 1.f, 1.f, 1.f, 1.f )
{
#ifdef eDEBUG_RENDER
  g_debugRenderer.addDrawable( * this );
#endif // eDebug_RENDER
}

//------------------------------------------------------------
EDebugDrawable::~EDebugDrawable()
{
#ifdef eDEBUG_RENDER
  g_debugRenderer.removeDrawable( * this );
#endif
}

//------------------------------------------------------------
void EDebugDrawable::startDebug()
{
  cASSERT( !m_bDebugStarted );
  m_bDebugStarted = true;
}

//------------------------------------------------------------
void EDebugDrawable::endDebug()
{
  cASSERT( m_bDebugStarted );
  m_bDebugStarted = false;
}

//------------------------------------------------------------
// EDebugRenderer
//------------------------------------------------------------

EDebugRenderer g_debugRenderer;

//------------------------------------------------------------
EDebugRenderer::EDebugRenderer() :
m_bDebugStarted( false )
{

}

//------------------------------------------------------------
void EDebugRenderer::startDebug()
{
  cASSERT( !m_bDebugStarted );
#ifdef eDEBUG_RENDER
  for( EDebugDrawableVector::iterator it = m_vecDrawables.begin(); 
    it != m_vecDrawables.end(); ++it )
  {
    (*it)->startDebug();
  }
#endif // eDEBUG_RENDER
  m_bDebugStarted = true;
}

//------------------------------------------------------------
void EDebugRenderer::endDebug()
{
  cASSERT( m_bDebugStarted );
  m_bDebugStarted = false;
#ifdef eDEBUG_RENDER
  for( EDebugDrawableVector::iterator it = m_vecDrawables.begin(); 
    it != m_vecDrawables.end(); ++it )
  {
    (*it)->endDebug();
  }
#endif // eDEBUG_RENDER
}

//------------------------------------------------------------
void EDebugRenderer::drawDebug()
{
  cASSERT( m_bDebugStarted );
#ifdef eDEBUG_RENDER
  for( EDebugDrawableVector::iterator it = m_vecDrawables.begin(); 
    it != m_vecDrawables.end(); ++it )
  {
    (*it)->drawDebug();
  }
#endif // eDEBUG_RENDER
}

//------------------------------------------------------------
SceneManager& EDebugRenderer::getCurrentOgreSceneManager() const
{
  cASSERT( m_pOgreSceneManager != NULL );
  return *m_pOgreSceneManager;
}

//------------------------------------------------------------
void EDebugRenderer::addDrawable(EDebugDrawable& drawable)
{
  m_vecDrawables.push_back( &drawable );
}

//------------------------------------------------------------
void EDebugRenderer::removeDrawable(const EDebugDrawable& drawable)
{
  for( EDebugDrawableVector::iterator it = m_vecDrawables.begin();
    it != m_vecDrawables.end(); ++it )
  {
    if( *it == &drawable )
    {
      m_vecDrawables.erase( it );
      break;
    }
  }
}

} // end namespace